<template>
  <div class="app-container">
    <el-card>
      <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
      >
        <el-form-item label="申请编号">
          <el-input
              v-model="queryParams.applyCode"
              placeholder="请输入申请编号"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="老人姓名">
          <el-input
              v-model="queryParams.applyName"
              placeholder="请输入老人姓名"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评估状态">
          <el-select
              v-model="queryParams.evaluationStatus"
              clearable
              placeholder="请选择状态"
              style="width: 240px"
          >
            <el-option
                v-for="dict in evaluation_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">

        <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="postList" stripe border max-height="500">
        <el-table-column label="申请编号" align="center" prop="applyCode"  width="200" />
        <el-table-column label="老人姓名" align="center" prop="applyName" width="100" />
        <el-table-column label="性别" align="center" width="100" >
          <template #default="scope">
        <span>
           {{sys_user_sex.find(item=>item.value==scope.row.sex)? sys_user_sex.find(item=>item.value==scope.row.sex).label:''}}

        </span>
          </template>
        </el-table-column>

        <el-table-column label="家庭住址" align="center" >
          <template #default="scope">
            <spna>{{scope.row.livingAddrProvinceName}} {{scope.row.livingAddrCityName}}{{scope.row.livingAddrCountyName}} {{scope.row.livingAddrStreetName}} {{scope.row.livingAddrCommunityName}}
              {{scope.row.livingDetailAddress}}
            </spna>
          </template>
        </el-table-column>
        <el-table-column label="评估状态" align="center" prop="commission" width="100" >
          <template #default="scope">
        <span>
           {{evaluation_status.find(item=>item.value==scope.row.evaluationStatus)? evaluation_status.find(item=>item.value==scope.row.evaluationStatus).label:''}}

        </span>
          </template>
        </el-table-column>
        <el-table-column label="评估时间" align="center" prop="evaluationDate"   width="180"/>
        <el-table-column label="评估机构" align="center" prop="evaluationAgencyName"  width="100"/>
        <el-table-column label="评估结果" align="center" prop="evaluationResultLabel"  width="100" />


        <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width" width="220">
          <template #default="scope">
            <el-button link type="primary"  @click="edit(scope.row)" v-if="scope.row.evaluationStatus=='0'"  >分配评估人员</el-button>
            <el-button link type="primary"  @click="editEvaluators(scope.row)" v-if="scope.row.evaluationStatus=='1'" >改派评估人员</el-button>
            <el-button link type="primary" @click="handinfo(scope.row)" v-if="scope.row.evaluationStatus=='3'">查看评估报告</el-button>
            <el-button link type="primary" @click="getHandInfo(scope.row)" v-if="scope.row.evaluationStatus=='3'||scope.row.evaluationStatus=='2'" >查看评估人员</el-button>

          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.limit"
          @pagination="getList"
      />
    </el-card>
    <el-dialog
        v-model="open"
        :title="title"
        append-to-body
        width="600px"
        v-if="open"
        @close="cancel"
        :close-on-click-modal="false"
    >
      <el-form ref="evaluationRef" :model="form" :rules="rules" label-width="120px" >
        <el-form-item label="评估模板" prop="evaluationTemplateId">
          <el-select v-model="form.evaluationTemplateId" clearable placeholder="请选择评估模板" style="width: 100%" :disabled="disableFlag">
            <el-option
                v-for="dict in TemplateData"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评估机构" prop="evaluationAgencyId"  >
          <el-select v-model="form.evaluationAgencyId" clearable placeholder="请选择评估机构" style="width: 100%" :disabled="disableFlag" >
            <el-option
                v-for="dict in AgencyList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评估人员" prop="evaluationAgencyPersonnelId">
          <el-input v-model="name" placeholder="请选择评估人员"  v-if="disableFlag" :disabled="true"/>

          <el-select v-model="form.evaluationAgencyPersonnelId" clearable placeholder="请选择评估人员" style="width: 100%" v-else>
            <el-option
                v-for="dict in PeopleList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormDebounceHandler">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="evaluation">


const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex,project_type,evaluation_status} = proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "project_type",
    'evaluation_status'

);
const router = useRouter();

import {
  getEvaluateAgencyPersonnelSelectOptions,
  getEvaluateAgencySelectOptions,
  getEvaluateTemplateSelectOptions,
  abilityEvaluate, allocateEvaluation, getEvaluationPersonnel
} from '@/api/homebedsign/index'
import { debounce } from "@/utils/debounce.js";

const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const title = ref("分配评估人员");

const total = ref(0);
const disableFlag=ref(false);
const TemplateData=ref([]);
const AgencyList =ref([])
const PeopleList=ref([])
const name=ref('')


const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    applyCode: undefined,
    applyName:undefined,
    evaluationStatus:undefined
  },
  rules: {
    evaluationTemplateId: [
      { required: true, message: "请选择评估模版", trigger: "change" },
    ],
    evaluationAgencyId: [
      { required: true, message: "请选择评估机构", trigger: "change" },
    ],
    evaluationAgencyPersonnelId: [
      { required: true, message: "请选择评估人员", trigger: "change" },
    ]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 提交按钮 */
const submitFormDebounceHandler = debounce(submitForm, 500);

function submitForm() {
  debugger
  proxy.$refs["evaluationRef"].validate((valid) => {
    if (valid) {
      allocateEvaluation(form.value).then(res=>{
        console.log('222222222',res)
        proxy.$modal.msgSuccess("分配成功");
        cancel()
        getList()
      })
    }
  });
}

function cancel() {
  open.value = false;
  reset();
}
function reset(){
  form.value={
    id:undefined,
    evaluationTemplateId:undefined,
    evaluationAgencyId:undefined,
    evaluationAgencyPersonnelId:undefined
  }
  disableFlag.value=false
  name.value=''
  proxy.resetForm("evaluationRef");
}
function edit(data) {
  // form.value = data;
  title.value = "分配评估人员";
  open.value = true;
  disableFlag.value = false;
  form.value.id=data.id
}
async function editEvaluators(data){
  console.log("【修改分配评估人员】", data)
  title.value = "修改分配评估人员";
  const data1= await getEvaluationPersonnel(data.id)
  form.value.id = data.id
  form.value.evaluationTemplateId=data1.evaluationPersonnel.evaluationTemplateId
  form.value.evaluationAgencyId=data1.evaluationPersonnel.evaluationAgencyId
  form.value.evaluationAgencyPersonnelId=data1.evaluationPersonnel.evaluationAgencyPersonnelId
  const data2= await getEvaluateAgencyPersonnelSelectOptions(data1.evaluationPersonnel.evaluationAgencyId)
  PeopleList.value= data2.evaluationAgencyPersonnelList


  open.value = true;
}
 async function getHandInfo(data){
   const data1= await getEvaluationPersonnel(data.id)
   form.value.evaluationTemplateId=data1.evaluationPersonnel.evaluationTemplateId
   form.value.evaluationAgencyId=data1.evaluationPersonnel.evaluationAgencyId
   const data2= await getEvaluateAgencyPersonnelSelectOptions(data1.evaluationPersonnel.evaluationAgencyId)
   PeopleList.value= data2.evaluationAgencyPersonnelList
   name.value=PeopleList.value.find(item=>item.id==data1.evaluationPersonnel.evaluationAgencyPersonnelId)?.name


   title.value = "查看分配评估人员";
  open.value = true;
  disableFlag.value = true;

}
/** 查询岗位列表 */
function getList() {
  loading.value = true;
  abilityEvaluate(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  });
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    applyCode: undefined,
    applyName:undefined,
    evaluationStatus:undefined
  };
  getList();
}
function handinfo(data){
  router.push({
    path:`/homebedsign/evaluation-info/info/${data.id}`,
  })
}




const stopWatch = watch(
    () => form.value.evaluationAgencyId,
    (newVal, oldVal) => {
      if(newVal){
          getEvaluateAgencyPersonnelSelectOptions(newVal).then(res=>{
          console.log("【res】 = ", res)
          PeopleList.value=res.evaluationAgencyPersonnelList
          // form.value.evaluationAgencyPersonnelId=''
        })
      }else {
        PeopleList.value=[]
        // form.value.evaluationAgencyPersonnelId=''
      }
    },
    { immediate: true }
);

onBeforeUnmount(() => {
  stopWatch();
});
function EvaluateAgencySelectOptions(){
  getEvaluateAgencySelectOptions().then(res=>{
    AgencyList.value=res.evaluationAgencyList
  })
}
function EvaluateTemplateSelectOptions(){
  getEvaluateTemplateSelectOptions().then(res=>{
     TemplateData.value=res.evaluationTemplateList
  })
}

getList();
EvaluateTemplateSelectOptions()
EvaluateAgencySelectOptions()
</script>
